package ahl;

import static org.junit.Assert.assertTrue;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;

/**
 * Tests whether or not a robot's heading turns at least once on a wall collision.
 * @author Arthur Lee
 */
public class TestRobotTurn extends RobotTestBed {
  boolean hitWall = false;
  boolean afterHitWall = false;
  boolean turnedRobot = false;
  double hitWallRobotHeading, afterHitWallRobotHeading;
  
  /**
   * Specifies that SittingDuck and BattleFire are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ahl.BattleFire";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * Grabs the robot heading before and after a wall collision.
   * @param event The event on an ended turn.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    //Retrieves all snapshots of robots and find if robot collision has occurred.
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    hitWall = robot.getState().isHitRobot();
    //Occurs on the turn after a wall collision.
    if (afterHitWall) {
      afterHitWallRobotHeading = robot.getGunHeading();
      afterHitWall = false;
      if (afterHitWallRobotHeading != hitWallRobotHeading) {
        turnedRobot = true;
      }
    }
    //Occurs on the turn that the wall collision occurred.
    if (hitWall) {
      hitWallRobotHeading = robot.getGunHeading();
      hitWall = false;
      afterHitWall = true;
    }
  }
  
  /**
   * Checks if the robot has turned at least once.
   * @param event The event on an ended battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Check if robot turned at least once after hitting wall", turnedRobot);
  }
}
